﻿using DannyFramework.Results;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;

namespace DannyFramework.Extenions
{
    /// <summary>
    /// 通用的结果和异常服务扩展
    /// </summary>
    public static class CommonResultExceptionServiceExtenions
    {

        /// <summary>
        /// 使用通用的结果和异常服务扩展
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddCommonResultException(this IServiceCollection services)
        {
            //2.配置CommonResultFilter过滤器
            services.Configure<MvcOptions>(options => {
                options.Filters.Add<CommonResultFilter>();
                options.Filters.Add<CommonExceptionFilter>();
            });

            //3.配置通用Json
            services.AddControllers().AddJsonOptions(options => {
                options.JsonSerializerOptions.PropertyNamingPolicy = null; //字段格式不变
            });

            return services;
        }
    }
}
